/*
 * Copyright user2010
 * All rights reserved
 */

package org.myorg.applitest.jdbc;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import java.sql.DriverManager;
import java.sql.ResultSet;


/**
 *
 * @author user
 */
public class DbConnectionSingleton {


    private  DbConnectionSingleton() throws Exception
    {
        Class.forName("com.mysql.jdbc.Driver");
        this._strUser_ = "none";
    }

    public static synchronized DbConnectionSingleton getSingletonObject() throws Exception {
        if (_singletonObject_ == null) {
                _singletonObject_ = new DbConnectionSingleton();
        }
        return _singletonObject_;
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        throw new CloneNotSupportedException();
    }

    public String getUser()
    {
        return this._strUser_;
    }

    public void connect(
            String strUrl,
            String strUser,
            String strPassword) throws Exception
    {
        this._strUser_ = strUser;
        this._con_ = (Connection) DriverManager.getConnection(
                this._url_,
                strUser,
                strPassword);
        this._stmt_ =    (Statement) this._con_.createStatement();
        
    }
    public void disconnect() throws Exception{
        this._con_.close();
        this._strUser_ = "none";
    }


    public void executeUpdate(String strCommand) throws Exception
    {
        if (!DEBUG){
            System.out.println(strCommand);
            this._stmt_.executeUpdate(strCommand);
        }
        else
            System.out.println(strCommand);

    }

    public void executeQuerry(String strCommand) throws Exception {
        if (!DEBUG){
            System.out.println(strCommand);
            this._res_ = this._stmt_.executeQuery(strCommand);
        }
        else
            System.out.println(strCommand);
    }
   public ResultSet getResult(){
       return _res_;

   }

    private static DbConnectionSingleton _singletonObject_;

    private Statement _stmt_;
    private String _url_ = "jdbc:mysql://localhost:3306/mysql";
    private Connection _con_;
    private String _strUser_;
    private ResultSet _res_;
    private Boolean DEBUG = false;


}
